﻿Namespace Catalog
    Public Class Release
        Inherits BaseObject
        Public Property ReleaseDate As Date
        Public Property PublishDate As Date
        Public Property Category As String
        Public Property Description As String
        Public Property CatalogNumber As String
        Public Property Price As Price
        Public Property TracksPrice As Price
        Public Property Label As Label
        Public Property Artists As List(Of Artist)
        Public Property Genres As List(Of Genre)
        Public Property Images As List(Of Image)

        Public Sub New()
            Me.Artists = New List(Of Artist)
            Me.Genres = New List(Of Genre)
            Me.Images = New List(Of Image)
        End Sub

        Public Shared Function FromBPObject(rel As BPORelease) As Release
            If rel Is Nothing Then Return Nothing
            Dim newRelease As New Release()

            With newRelease
                .ID = rel.id
                .Name = rel.name
                .Slug = rel.slug
                Date.TryParseExact(rel.releaseDate, "yyyy-MM-dd", Nothing, Globalization.DateTimeStyles.None, .ReleaseDate)
                Date.TryParseExact(rel.publishDate, "yyyy-MM-dd", Nothing, Globalization.DateTimeStyles.None, .PublishDate)
                .Category = rel.category
                .Description = rel.description
                .CatalogNumber = rel.catalogNumber
                .Price = Price.FromBPObject(rel.price)
                .TracksPrice = Price.FromBPObject(rel.tracksPrice)
                .Label = New Label() With {.ID = rel.label.id, .Name = rel.label.name, .Slug = rel.label.slug}

                For Each a As BPOBase In rel.artists
                    .Artists.Add(New Artist() With {.ID = a.id, .Name = a.name, .Slug = a.slug})
                Next

                For Each g As BPOGenreBase In rel.genres
                    .Genres.Add(New Genre() With {.ID = g.id, .Name = g.name, .Slug = g.slug})
                Next

                For Each i As KeyValuePair(Of String, BPOImage) In rel.images
                    .Images.Add(Image.FromBPObject(i))
                Next
            End With

            Return newRelease
        End Function
    End Class
End Namespace